package com.yiwei.hdb.api.requestInfo;

import com.yiwei.hdb.api.Api;
import com.yiwei.hdb.api.bean.ChsiPwdTypeBean;
import com.yiwei.hdb.api.bean.RetrieveByEmailBean;
import com.yiwei.hdb.api.bean.RetrieveByPhone2Bean;
import com.yiwei.hdb.api.bean.RetrieveByPhoneBean;
import com.yiwei.hdb.api.callback.ChsiForgetContract;
import com.yiwei.hdb.util.Network;
import com.yiwei.hdb.util.RxLifeUtil;
import com.yiwei.hdb.util.ToastUtil;

/**
 * Created by Administrator on 2017/5/14.
 */

public class ChsiForgetPwdRequest extends BaseRequest<ChsiForgetContract.View> implements ChsiForgetContract.Presenter<ChsiForgetContract.View> {

    public static ChsiForgetPwdRequest create() {
        return new ChsiForgetPwdRequest();
    }

    //    图形码
    @Override
    public void getChsiImgCodeInfo(boolean isfirst, int type) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        if(!isfirst){
            mView.onRequestStart();
        }

        Api.api_service.getChsiImgCodeInfo().compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getChsiImgCodeInfoSuccess(type, result);
                });
    }

    /**
     * 找回密码方式
     */
    @Override
    public void getChsiPwdTypeInfo(String username, String captcha) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        ChsiPwdTypeBean bean = new ChsiPwdTypeBean();
        bean.username = username;
        bean.captcha = captcha;
        Api.api_service.getChsiPwdTypeInfo(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getChsiPwdTypeInfoSuccess(result);
                });
    }

    /**
     * 通过email找回密码
     */
    @Override
    public void getRetrieveByEmailInfo(String ctoken, String email, String realname, String idno) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        RetrieveByEmailBean bean = new RetrieveByEmailBean();
        bean.ctoken = ctoken;
        bean.email = email;
        bean.idno = idno;
        bean.name = realname;
        Api.api_service.getRetrieveByEmailInfo(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getRetrieveByEmailInfoSuccess(result);
                });
    }

    /**
     * 通过手机找回密码
     */
    @Override
    public void getRetrieveByPhoneInfo(String ctoken, String mobile, String realname, String idno, String captcha) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        RetrieveByPhoneBean bean = new RetrieveByPhoneBean();
        bean.ctoken = ctoken;
        bean.mobile = mobile;
        bean.captcha = captcha;
        bean.idno = idno;
        bean.name = realname;
        Api.api_service.getRetrieveByPhoneInfo(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getRetrieveByPhoneInfoSuccess(result);
                });
    }

    /**
     * 通过手机找回密码
     */
    @Override
    public void getRetrieveByPhoneInfo2(String clst, String vcode, String password) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        RetrieveByPhone2Bean bean = new RetrieveByPhone2Bean();
        bean.clst = clst;
        bean.vcode = vcode;
        bean.password = password;
        Api.api_service.getRetrieveByPhoneInfo2(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (result.getStatus() == 0) {
                        mView.showError();
                    }
                    mView.getRetrieveByPhoneInfo2Success(result);
                });
    }
}
